package com.diy.sigmund.diycommon.test.controller.apollo;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.middleware.cache.redis.RedissonConfig;
import com.diy.sigmund.diycommon.test.entity.RedissonDTO;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ylm-sigmund
 * @since 2021/11/22 10:38
 */
@RestController
public class RedissonConfigChangeController {

    private static final Logger log = LoggerFactory.getLogger(RedissonConfigChangeController.class);

    @Resource
    private RedissonClient customRedisson;

    /**
     * @see org.redisson.api.RLock#tryLock(long, long, java.util.concurrent.TimeUnit)
     * 尝试使用定义的leaseTime获取锁。如果需要，等待到定义的waitTime，直到锁可用为止。锁定将在定义的leaseTime间隔后自动释放。
     * 如果主动设置了锁的过期时间,Redisson不会自动续期
     */
    @PostMapping("/redisson/testApolloRefresh")
    public ResultMsg test1(@RequestBody RedissonDTO redissonDTO) throws InterruptedException {
        // hget 锁名称 UUID:当前线程id
        // hget 锁名称 ffc7931f-d25c-4977-a532-e0c88df3e311:45
        RedissonClient redissonClient = RedissonConfig.getRedisson();
        log.info("地址,id：{},{}", redissonClient.getConfig().useSingleServer().getAddress(), redissonClient.getId());
        RLock lock = redissonClient.getLock(redissonDTO.getName());

        lock.lock();
        // boolean b = lock.tryLock(3000, 120_000, TimeUnit.MILLISECONDS);
        // log.info("{}加锁结果：{}", redissonDTO.getName(), b);
        // RLock rLock = RedissonDistributedLock.tryLock(redissonDTO.getName());
        // TimeUnit.SECONDS.sleep(1);
        // RedissonDistributedLock.unlock(rLock);
        lock.unlock();
        return ResultMsg.success();
    }

    @PostMapping("/redisson/testApolloRefresh1")
    public ResultMsg test2(@RequestBody RedissonDTO redissonDTO) throws InterruptedException {
        // hget 锁名称 UUID:当前线程id
        // hget 锁名称 ffc7931f-d25c-4977-a532-e0c88df3e311:45
        RLock lock = customRedisson.getLock(redissonDTO.getName());
        log.info("地址,id：{},{}", customRedisson.getConfig().useSingleServer().getAddress(), customRedisson.getId());
        boolean b = lock.tryLock(3000, 120_000, TimeUnit.MILLISECONDS);
        log.info("{}加锁结果：{}", redissonDTO.getName(), b);
        // RLock rLock = RedissonDistributedLock.tryLock(redissonDTO.getName());
        // TimeUnit.SECONDS.sleep(1);
        // RedissonDistributedLock.unlock(rLock);
        lock.unlock();
        return ResultMsg.success();
    }

    @PostMapping("/redisson/getBloomFilter")
    public ResultMsg getBloomFilter(@RequestBody RedissonDTO redissonDTO) throws InterruptedException {
        RBloomFilter<String> bloomFilter = customRedisson.getBloomFilter("phoneList");
        //初始化布隆过滤器：预计元素为100000000L,偏差率为3%
        bloomFilter.tryInit(100000000L, 0.03);
        //将号码10086插入到布隆过滤器中
        bloomFilter.add("10086");

        //判断下面号码是否在布隆过滤器中
        //输出false
        System.out.println("bloomFilter.contains(\"123456\"):" + bloomFilter.contains("123456"));
        //输出true
        System.out.println("bloomFilter.contains(\"10086\"):" + bloomFilter.contains("10086"));
        // bloomFilter.contains("123456"):false
        // bloomFilter.contains("10086"):true
        return ResultMsg.success();
    }

}
